package com.nefu.editor.web;

import com.nefu.editor.common.EdiException;
import com.nefu.editor.common.ErrorMessage;
import com.nefu.editor.common.RestData;
import com.nefu.editor.common.util.TokenUtil;
import com.nefu.editor.core.model.Notice;
import com.nefu.editor.core.model.User;
import com.nefu.editor.service.CartService;
import com.nefu.editor.service.CollectsService;
import com.nefu.editor.service.NoticeService;
import com.nefu.editor.service.OrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author chenchenT
 * @date : 18-11-13
 * @since : java 8
 */
@CrossOrigin(origins = "*", allowCredentials = "true", allowedHeaders = "*")
@RestController
public class UserApi {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    private final CollectsService collectsService;
    private final CartService cartService;
    private final OrderService orderService;
    private final NoticeService noticeService;

    @Autowired
    public UserApi(CollectsService collectsService, CartService cartService, OrderService orderService, NoticeService noticeService) {
        this.collectsService = collectsService;
        this.cartService = cartService;
        this.orderService = orderService;
        this.noticeService = noticeService;
    }

    @RequestMapping(value = "/collectList/{userSystemId}", method = RequestMethod.GET)
    public RestData getCollectList(@PathVariable(value = "userSystemId") Integer userSystemId, HttpServletRequest request) {
        logger.info("collectList : ");
        User currentUser = TokenUtil.getUserByToken(request);
        if (null != currentUser) {
            try {
                User user = new User();
                user.setSystemId(userSystemId);
                List<Map<String, Object>> articleList = collectsService.getArticlesByUserSystemId(user);
                return new RestData(articleList);
            } catch (EdiException e) {
                return new RestData(1, e.getMessage());
            }
        } else {
            return new RestData(2, ErrorMessage.PLEASE_RELOGIN);
        }
    }

    @RequestMapping(value = "/cart/{userSystemId}", method = RequestMethod.GET)
    public RestData getCartList(@PathVariable(value = "userSystemId") Integer userSystemId, HttpServletRequest request) {
        logger.info("collectList : ");
        User currentUser = TokenUtil.getUserByToken(request);
        if (null != currentUser) {
            try {
                User user = new User();
                user.setSystemId(userSystemId);
                List<Map<String, Object>> carts = cartService.getCartByUserSystemId(user);
                return new RestData(carts);
            } catch (EdiException e) {
                return new RestData(1, e.getMessage());
            }
        } else {
            return new RestData(2, ErrorMessage.PLEASE_RELOGIN);
        }
    }

    @RequestMapping(value = "/order/{userSystemId}", method = RequestMethod.GET)
    public RestData getOrderList(@PathVariable(value = "userSystemId") Integer userSystemId, HttpServletRequest request) {
        logger.info("collectList : ");
        User currentUser = TokenUtil.getUserByToken(request);
        if (null != currentUser) {
            try {
                User user = new User();
                user.setSystemId(userSystemId);
                List<Map<String, Object>> orders = orderService.getOrderByUserSystemId(user);
                return new RestData(orders);
            } catch (EdiException e) {
                return new RestData(1, e.getMessage());
            }
        } else {
            return new RestData(2, ErrorMessage.PLEASE_RELOGIN);
        }
    }

    @RequestMapping(value = "/noticeDetail/{systemId}", method = RequestMethod.GET)
    public RestData getNoticeDetail(@PathVariable(value = "systemId") Integer systemId, HttpServletRequest request) {
        logger.info("collectList : ");
        User currentUser = TokenUtil.getUserByToken(request);
        if (null != currentUser) {
            try {
                Notice notice = new Notice();
                notice.setSystemId(systemId);
               Map<String, Object> map = noticeService.getNoticeDetailByNoticeSystemId(notice);
                return new RestData(map);
            } catch (EdiException e) {
                return new RestData(1, e.getMessage());
            }
        } else {
            return new RestData(2, ErrorMessage.PLEASE_RELOGIN);
        }
    }

}
